{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "高数笔记 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一、向量的概念"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "客观世界中有这样一类量,它们既有大小,又有方向,例如位移、速度、加速度、力、力矩等等,这一类量叫做向量(或矢量)。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "二、向量的线性运算\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.向量的加减法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "设有两个向量a与b,任取一点A,作AB=a,再以B为起点,作BC=b,连接AC，那么向量AC=c称为向量a与b的和,记作a+b,即c =q +b\n",
    "上述作出两向量之和的方法叫做向量相加的三\n",
    "角形法则。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.向量与数的乘法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "向量与数的乘积符合下列运算规律:（1）结合律（2）分配律"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例1  在平行四边形 ABCD 中,设AB =a,AD =b.试用a和b表示向量MÁ MB、MC和MD。这里M是平行四边形对角线的交点。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "向量MA = -0.5*a - 0.5*b\n",
      "向量MB = 0.5*a - 0.5*b\n",
      "向量MC = 0.5*a + 0.5*b\n",
      "向量MD = -0.5*a + 0.5*b\n"
     ]
    }
   ],
   "source": [
    "import sympy\n",
    "\n",
    "# 定义向量符号\n",
    "a = sympy.Symbol('a')\n",
    "b = sympy.Symbol('b')\n",
    "\n",
    "# 因为平行四边形对角线互相平分，M是对角线交点，所以有\n",
    "# 向量MA等于负的向量MC，向量MB等于负的向量MD\n",
    "# 先求向量AC和向量BD\n",
    "AC = a + b\n",
    "BD = b - a\n",
    "\n",
    "# 向量MA等于负的二分之一向量AC\n",
    "MA = -1 / 2 * AC\n",
    "# 向量MB等于负的二分之一向量BD\n",
    "MB = -1 / 2 * BD\n",
    "# 向量MC等于二分之一向量AC\n",
    "MC = 1 / 2 * AC\n",
    "# 向量MD等于二分之一向量BD\n",
    "MD = 1 / 2 * BD\n",
    "\n",
    "print(\"向量MA =\", MA)\n",
    "print(\"向量MB =\", MB)\n",
    "print(\"向量MC =\", MC)\n",
    "print(\"向量MD =\", MD)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例2 求解已知两点A(1，2，5)和 B(2，6，7)以及金实数x≠-1,在直线AB上求点M,使\n",
    "AM =MB."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "满足条件的点M的坐标为: (1.75, 5.0, 6.5)\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入的lambda值（确保lambda不等于-1）\n",
    "lambda_value = float(input(\"请输入lambda值（lambda不等于-1）: \"))\n",
    "while lambda_value == -1:\n",
    "    print(\"lambda值不能为-1，请重新输入！\")\n",
    "    lambda_value = float(input(\"请输入lambda值（lambda不等于-1）: \"))\n",
    "\n",
    "# 根据向量关系建立方程组求解M点的横坐标x\n",
    "x = (1 + 2 * lambda_value) / (1 + lambda_value)\n",
    "# 根据向量关系建立方程组求解M点的纵坐标y\n",
    "y = (2 + 6 * lambda_value) / (1 + lambda_value)\n",
    "# 根据向量关系建立方程组求解M点的竖坐标z\n",
    "z = (5 + 7 * lambda_value) / (1 + lambda_value)\n",
    "\n",
    "print(f\"满足条件的点M的坐标为: ({x}, {y}, {z})\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "三、向量的模、方向角、投影"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例3 求证以M(4,3,1)、M,(7,1,2)、M(5,2,3)三点为顶点的三角形是一个等腰三角形."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "以M1、M2、M3三点为顶点的三角形是一个等腰三角形\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "\n",
    "# 计算两点之间距离的函数\n",
    "def distance(point1, point2):\n",
    "    \"\"\"\n",
    "    根据两点坐标计算两点间的距离\n",
    "    :param point1: 第一个点的坐标，格式为(x, y, z)\n",
    "    :param point2: 第二个点的坐标，格式为(x, y, z)\n",
    "    :return: 两点间的距离\n",
    "    \"\"\"\n",
    "    x1, y1, z1 = point1\n",
    "    x2, y2, z2 = point2\n",
    "    return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2)\n",
    "\n",
    "\n",
    "# 定义三个点的坐标\n",
    "M1 = (4, 3, 1)\n",
    "M2 = (7, 1, 2)\n",
    "M3 = (5, 2, 3)\n",
    "\n",
    "# 计算三条边的长度\n",
    "side1 = distance(M1, M2)\n",
    "side2 = distance(M2, M3)\n",
    "side3 = distance(M3, M1)\n",
    "\n",
    "# 判断是否有两条边相等\n",
    "if side1 == side2 or side2 == side3 or side1 == side3:\n",
    "    print(\"以M1、M2、M3三点为顶点的三角形是一个等腰三角形\")\n",
    "else:\n",
    "    print(\"以M1、M2、M3三点为顶点的三角形不是一个等腰三角形\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例4 在z轴上求与两点A(-4,1,7)和B(3,5,-2)等距离的点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在z轴上与A、B两点等距离的点为(0, 0, -99.99999925494194)\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "# 定义点A的坐标\n",
    "A = (-4, 1, 7)\n",
    "# 定义点B的坐标\n",
    "B = (3, 5, -2)\n",
    "\n",
    "# 根据距离相等建立方程求解z\n",
    "left_side = lambda z: (-4 - 0) ** 2 + (1 - 0) ** 2 + (7 - z) ** 2\n",
    "right_side = lambda z: (3 - 0) ** 2 + (5 - 0) ** 2 + (-2 - z) ** 2\n",
    "\n",
    "# 利用二分法来寻找满足方程的z值，这里假设z的范围大致在[-100, 100]，可按需调整\n",
    "low = -100\n",
    "high = 100\n",
    "while high - low > 1e-6:  # 设置精度，可根据需求改变\n",
    "    mid = (low + high) / 2\n",
    "    if left_side(mid) < right_side(mid):\n",
    "        low = mid\n",
    "    elif left_side(mid) > right_side(mid):\n",
    "        high = mid\n",
    "    else:\n",
    "        break\n",
    "print(f\"在z轴上与A、B两点等距离的点为(0, 0, {mid})\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例5 已知两点A(4,0,5)和B(7,1,3),求与AB方向相同的单位向量e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "与AB方向相同的单位向量e为: (0.8017837257372732, 0.2672612419124244, -0.5345224838248488)\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "# 定义点A的坐标\n",
    "A = (4, 0, 5)\n",
    "# 定义点B的坐标\n",
    "B = (7, 1, 3)\n",
    "\n",
    "# 计算向量AB的坐标表示\n",
    "vector_AB = (B[0] - A[0], B[1] - A[1], B[2] - A[2])\n",
    "\n",
    "# 计算向量AB的模\n",
    "vector_AB_magnitude = math.sqrt(vector_AB[0] ** 2 + vector_AB[1] ** 2 + vector_AB[2] ** 2)\n",
    "\n",
    "# 计算单位向量e的坐标表示\n",
    "unit_vector_e = (vector_AB[0] / vector_AB_magnitude, vector_AB[1] / vector_AB_magnitude, vector_AB[2] / vector_AB_magnitude)\n",
    "\n",
    "print(f\"与AB方向相同的单位向量e为: {unit_vector_e}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
